package com.yanqu.road.server.cmd.monopolymarket.res;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.monopolymarket.config.MonopolyMarketResConfigData;
import com.yanqu.road.entity.monopolymarket.office.log.MonopolyMarketUserPhysicalChangeLogData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.MonopolyMarketRankActivityMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/20 10:25
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_RES_GRAB_ACTION_FROM_CROSS, desc = "资源争夺战撤销/抢夺 位置 跨服返回区服 协议")
public class MonopolyMarketResGrabActionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketResGrabActionToGameServerMsg resp = MonopolyMarketProto.MonopolyMarketResGrabActionToGameServerMsg.parseFrom(packet.getBytes());
        Property property = null;
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        if(resp.getActionType() == 0){
            // 解除预扣除
            if(marketModule.resGrabNeedConsumeGold > 0){
                property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(marketModule.resGrabNeedConsumeGold));
                player.getModule(PreReduceModule.class).restorePreReduce(property);
                marketModule.resGrabNeedConsumeGold = 0;
            }
        }
        if(resp.getStcMsg().getRet() == 0){
            MonopolyMarketProto.MonopolyMarketResGrabActionRespMsg stcMsg = resp.getStcMsg();
            if(resp.getActionType() == 0){
                // 入座 消耗元宝 消耗体力 加派遣次数
                if(property != null){
                    player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.MonopolyMarket, eLogMoneyType.MonopolyMarketOfficeConsume);
                }
                int oldValue = marketModule.baseData.getPhysical();
                int logType = 0;
                if(resp.getStcMsg().getIsSuccess()){
                    // 成功抢夺
                    marketModule.addDispatchInfo(resp.getType(),resp.getPosIndex(),stcMsg.getObjectListList(),stcMsg.getPointInfo().getStartTime());
                    // 加掠夺奖励
                    Property robReward = MonopolyMarketMgr.addReward(resp.getType(),resp.getRewardNum());
                    player.getModule(CurrencyModule.class).addCurrency(robReward,eLogMoneyType.MonopolyMarket,eLogMoneyType.MonopolyMarketResGrabRobReward);
                    // 成功入座 扣除完整的体力
                    marketModule.baseData.setPhysical(marketModule.baseData.getPhysical() - marketModule.resGrabNeedPhysical);
                    logType = 1;
                }else {
                    // 抢夺失败 扣除部分体力
                    Map<Integer, MonopolyMarketResConfigData> resConfigDataMap = MonopolyMarketMgr.resConfigLevelMap.get(resp.getType());
                    MonopolyMarketResConfigData resConfigData = resConfigDataMap.get(marketModule.level);
                    int consumePhysical = 0;
                    if(resp.getStcMsg().getPointStatus() == 0){
                        // 占领npc 失败
                        consumePhysical = (marketModule.resGrabNeedPhysical * (1000 -  resConfigData.getNpcReturncost())) / 1000;
                    }else {
                        // 占领玩家据点 失败
                        consumePhysical = (marketModule.resGrabNeedPhysical * (1000 -  resConfigData.getReturncost())) / 1000;
                    }
                    marketModule.baseData.setPhysical(marketModule.baseData.getPhysical() - consumePhysical);
                    logType = 2;
                }
                LogMgr.addMonopolyMarketUserPhysicalChangeLogData(new MonopolyMarketUserPhysicalChangeLogData(player.getUserId(),marketModule.baseData.getOfficeCurPos(),marketModule.baseData.getOfficeLevel(),marketModule.baseData.getOfficeNo(),logType,oldValue,marketModule.baseData.getPhysical(), DateHelper.getCurrentSecond()));
            }else {
                // 撤回
                MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg settlement = resp.getSettlement();
                Property reward = MonopolyMarketMgr.addReward(resp.getType(),resp.getRewardNum());
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.MonopolyMarket,eLogMoneyType.MonopolyMarketResGrabRecallReward);

                MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.Builder builder  = MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.newBuilder();
                builder.setType(2);
                builder.addResGrabTip(MonopolyMarketMgr.builderMonopolyMarketResGrabTipEntity(settlement));
                player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_MONOPOLY_MARKET_BROADCAST, builder));

                MonopolyMarketMgr.leaveResGrabPoint(player,resp.getType(),settlement.getPosIndex(),false);
            }
            // 加入冲榜
            MonopolyMarketRankActivityMgr.addRankValue(player.getUserId(), resp.getRewardNum());

            MonopolyMarketMgr.refreshUser(player,false,true);
        }
        player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_RES_GRAB_ACTION, resp.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
